package com.tracy.lifeproject.discovery.goods.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.bean.GoodsBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.discovery.goods.view.KeyWordsView;


import java.util.List;

/**
 * Created by ZJS on 2016/7/20.
 */
public class GoodsRecyclerAdapter extends RecyclerView.Adapter<GoodsRecyclerAdapter.MyViewHolder> {

    private List<GoodsBean.DataBean.CategoriesBean> mDatas;
    private Context mContext;

    public GoodsRecyclerAdapter(Context context){
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
//        Log.i("zjs","==========================================");
        GoodsBean.DataBean.CategoriesBean categoriesBean = mDatas.get(position);
        Picasso.with(mContext).load(categoriesBean.getIcon_url()).into(holder.iv);
        List<GoodsBean.DataBean.CategoriesBean.SubcategoriesBean> subcategories = categoriesBean.getSubcategories();
        int imageId = categoriesBean.getId();
        final String imageUrl  = String.format(UrlString.URL_GOODS_ITEM_IMG,imageId);
        final String imageName = categoriesBean.getName();
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListencer.onItemClickd(imageUrl,imageName,holder.iv);
            }
        });

        holder.keyWordsView.removeAllViews();
        for (int i = 0; i <subcategories.size() ; i++) {
            final TextView textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.textview_goodsitem,null);
            final String menuItem = subcategories.get(i).getName();
            textView.setText(menuItem);

            int txtId = subcategories.get(i).getId();
            final String txtUrl  = String.format(UrlString.URL_GOODS_ITEM,txtId);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListencer.onItemClickd(txtUrl,menuItem,textView);
                }
            });

            holder.keyWordsView.addView(textView);
        }
//        Log.i("zjs","height"+holder.keyWordsView.getHeight());
    }

    @Override
    public int getItemCount() {
//        Log.i("zjs","-------------------->"+mDatas);
        return mDatas==null?0:mDatas.size();
    }

    public void setDatas(List<GoodsBean.DataBean.CategoriesBean> datas){
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        private KeyWordsView keyWordsView;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_goodsItem);
            keyWordsView = (KeyWordsView) itemView.findViewById(R.id.keyWordsView_item);
        }
    }

    private OnKeyWordViewItemClickListencer mListencer;

    public void setOnKeyWordViewItemClickListencer(OnKeyWordViewItemClickListencer listencer){
        this.mListencer = listencer;
    }

    public interface OnKeyWordViewItemClickListencer{
        void onItemClickd(String url,String itemName, View view);
    }
}
